import { Directive, Input, OnChanges, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { FormConfiguration, FormOptions } from '../models/forms.model';

@Directive()
export class FormDefaultComponent implements OnInit, OnChanges {
    public configuration: FormConfiguration = {
        formControlId: '',
        formGroupReference: null,
        qaPrefix: '',
        options: null,
        iconLabel: null,
        showArrow: true,
        required: false,
        maxlength: 50,
        minlength: 0,
        step: 1,
        showLabel: true,
        translationKey: null,
        placeholderTranslationKey: null,
        column: false,
        ignoreDotsOnCount: false,
        inputClass: '',
    };
    public currentMaxLength!: number;
    public isDisabled = false;
    public isEmpty = false;

    @Input() set ignoreDotsOnCount(ignoreDotsOnCount: boolean) {
        this.configuration.ignoreDotsOnCount = ignoreDotsOnCount;
    }

    @Input() set formControlId(formControlId: string) {
        this.configuration.formControlId = formControlId;
    }

    @Input() set inputClass(inputClass: string) {
        this.configuration.inputClass = inputClass;
    }

    @Input() set formGroupReference(formGroupReference: FormGroup) {
        this.configuration.formGroupReference = formGroupReference;
    }

    @Input() set showLabel(showLabel: boolean) {
        this.configuration.showLabel = showLabel;
    }

    @Input() set column(column: boolean) {
        this.configuration.column = column;
    }

    @Input() set qaPrefix(qaPrefix: string) {
        this.configuration.qaPrefix = qaPrefix;
    }

    @Input() set showArrow(showArrow: boolean) {
        this.configuration.showArrow = showArrow;
    }

    @Input() set translationKey(translationKey: string) {
        this.configuration.translationKey = translationKey;
    }

    @Input() set attributes(configuration: any) {
        this.configuration = { ...this.configuration, ...configuration };
    }

    @Input() set iconLabel(iconLabel: string) {
        this.configuration.iconLabel = iconLabel;
    }

    @Input() set required(required: boolean) {
        this.configuration.required = required;
    }

    @Input() set maxlength(maxlength: number) {
        this.configuration.maxlength = maxlength;
    }

    @Input() set minlength(minlength: number) {
        this.configuration.minlength = minlength;
    }

    @Input() set step(step: number) {
        this.configuration.step = step;
    }

    @Input() set options(options: FormOptions[]) {
        this.configuration.options = options;
    }

    @Input() set placeholderTranslationKey(translationKey: string) {
        this.configuration.placeholderTranslationKey = translationKey;
    }

    constructor() {}

    ngOnInit() {
        this.currentMaxLength = this.configuration.maxlength as number;
        if (this.configuration.ignoreDotsOnCount) {
            const { formGroupReference, formControlId, maxlength } = this.configuration;
            formGroupReference
                ?.get(formControlId)
                ?.valueChanges.subscribe(
                    (value: string) =>
                        (this.currentMaxLength = (maxlength || 0) + (value?.match(/\./g) ? value.match(/\./g)?.length || 0 : 0))
                );
        }
    }

    ngOnChanges() {
        const { formGroupReference, formControlId } = this.configuration;
        this.isDisabled = !!formGroupReference?.get(formControlId)?.disabled;
        this.isEmpty = !formGroupReference?.get(formControlId)?.value;
    }

    public get getPlaceholderTranslationKey() {
        if (this.isDisabled && this.isEmpty) return 'interface.dash';
        return this.configuration.placeholderTranslationKey || this.labelTranslationKey;
    }

    public get currentValue() {
        const { formGroupReference, formControlId } = this.configuration;
        return formGroupReference?.get(formControlId)?.value;
    }

    public get labelTranslationKey() {
        const { translationKey, formControlId } = this.configuration;
        return translationKey || 'form.labels.' + formControlId;
    }
}
